package com.devunion.bogdan.view.wizards;

import org.netbeans.spi.wizard.WizardPage;
import org.netbeans.spi.wizard.Wizard;
import org.netbeans.spi.wizard.WizardException;
import org.netbeans.api.wizard.WizardDisplayer;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.util.*;
import java.util.List;
import java.awt.*;

import com.devunion.bogdan.view.util.DialogHelper;
import com.devunion.bogdan.model.project.info.ProjectInfo;

public class NewProjectWizard {
    public static final String PROJECT_NAME = "PROJECT_NAME";

    public static final String DEFAULT_PROJECT_NAME = "Untitled";

    private Wizard wiz;
    private List<ProjectInfo> projectInfos;

    public Map show() {
        if (wiz == null) {
            wiz = NewProjectPage.createWizard("Create New Project", new WizardPage[]{getNewProjectPage()}, new ResultProducer());
        }

        return (Map) WizardDisplayer.showWizard(wiz);
    }

    private WizardPage getNewProjectPage() {
        final JTextField jTextFieldProjectName = new JTextField(DEFAULT_PROJECT_NAME);
        jTextFieldProjectName.setName(PROJECT_NAME);

        WizardPage result = new WizardPage("step1", "Specify project name") {
            protected String validateContents(Component component, Object event) {
                boolean isProjectNameSet = !StringUtils.isBlank(jTextFieldProjectName.getText());

                if (!isProjectNameSet) {
                    return "You need to specify project name";
                }

                for (ProjectInfo info : projectInfos) {
                    if (info.getName().equals(jTextFieldProjectName.getText())) {
                        return "Porject with the same name already exists";
                    }
                }

                return null;
            }
        };

        result.setLayout(new GridBagLayout());
        result.setPreferredSize(new Dimension(640, 480));

        DialogHelper.placeComponent(result, new JLabel("Project name"), 0, 0, 1, 1,
                0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
                0, 0, 0, 0);

        DialogHelper.placeComponent(result, jTextFieldProjectName, 0, 1, 1, 1,
                1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
                4, 0, 0, 16);

        DialogHelper.placeComponent(result, Box.createVerticalGlue(), 0, 2, 1, 1,
                0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL,
                0, 0, 0, 0);

        return result;
    }

    public void setProjectInfos(List<ProjectInfo> projectInfos) {
        this.projectInfos = projectInfos;
    }

    private class ResultProducer implements WizardPage.WizardResultProducer {
        public Object finish(Map wizardData) throws WizardException {
            return wizardData;
        }

        public boolean cancel(Map map) {
            return true;
        }
    }
}


